<?
require_once("include/bittorrent.php");
dbconn(false);
loggedinorreturn();

$res = query("SELECT COUNT(*) FROM messages WHERE receiver=" . $CURUSER["id"] . " AND location IN ('in', 'both')") or print(mysql_error());
$arr = mysql_fetch_row($res);
$messages = $arr[0];
$res = query("SELECT COUNT(*) FROM messages WHERE receiver=" . $CURUSER["id"] . " AND location IN ('in', 'both') AND unread='yes'") or print(mysql_error());
$arr = mysql_fetch_row($res);
$unread = $arr[0];
$res = query("SELECT COUNT(*) FROM messages WHERE sender=" . $CURUSER["id"] . " AND location IN ('out', 'both')") or print(mysql_error());
$arr = mysql_fetch_row($res);
$outmessages = $arr[0];

stdhead($CURUSER["username"] . "'s private page");

if ($_GET["edited"]) {
	print("<h1>Profile updated!</h1>\n");
	if ($_GET["mailsent"])
		print("<h2>Confirmation email has been sent!</h2>\n");
}
elseif ($_GET["emailch"])
	print("<h1>Email address changed!</h1>\n");
else
	print("<h1>Welcome, <a href=userdetails.php?id=$CURUSER[id]>$CURUSER[username]</a>!</h1>\n");

?>
<table border="1" cellspacing="0" cellpadding="10" align="center">
<tr>
<td align="center" width="33%"><a href=logout.php><b>Logout</b></a></td>
<td align="center" width="33%"><a href=mytorrents.php><b>My torrents</b></a></td>
<td align="center" width="33%"><a href=friends.php><b>My users lists</b></a></td>
</tr>
<tr>
<td colspan="3">
<form method="post" action="takeprofedit.php">
<table border="1" cellspacing=0 cellpadding="5" width="100%">
<?

/***********************

$res = query("SELECT COUNT(*) FROM ratings WHERE user=" . $CURUSER["id"]);
$row = mysql_fetch_array($res);
tr("Ratings submitted", $row[0]);

$res = query("SELECT COUNT(*) FROM comments WHERE user=" . $CURUSER["id"]);
$row = mysql_fetch_array($res);
tr("Written comments", $row[0]);

****************/

$res = query("SELECT id, name FROM stylesheets ORDER BY id") or die;
while ($arr = mysql_fetch_array($res))
  $stylesheets .= "<option value=$arr[id]" . ($CURUSER["stylesheet"] == $arr['id'] ? " selected" : "") . ">$arr[name]</option>\n";

$residences = "<option value=0>None</option>\n";
$res = query("SELECT id, name FROM residences ORDER BY id") or die;
while ($arr = mysql_fetch_array($res))
  $residences .= "<option value=$arr[id]" . ($CURUSER["residence"] == $arr['id'] ? " selected" : "") . ">$arr[name]</option>\n";

$faculties = "<option value=0>None</option>\n";
$res = query("SELECT id, name FROM faculties ORDER BY id") or die;
while ($arr = mysql_fetch_array($res))
  $faculties .= "<option value=$arr[id]" . ($CURUSER["faculty"] == $arr['id'] ? " selected" : "") . ">$arr[name]</option>\n";

$res = query("SELECT id, name FROM logos ORDER BY id") or die;
while ($arr = mysql_fetch_array($res))
  $logos .= "<option value=$arr[id]" . ($CURUSER["logo"] == $arr['id'] ? " selected" : "") . ">$arr[name]</option>\n";

tr("Account parked",
"<input type=radio name=parked" . ($CURUSER["parked"] == "yes" ? " checked" : "") . " value=yes>Yes
<input type=radio name=parked" .  ($CURUSER["parked"] == "no" ? " checked" : "") . " value=no>No
<br><font class=small size=1>You should park your account to prevent it from being deleted because of inactivity if<br>you're leaving residence temporarily. When the account has been parked limits are put<br>on the account, for example you cannot use the tracker and browse some of the pages.</font>"
,1);

tr("Accept PMs",
"<input type=radio name=acceptpms" . ($CURUSER["acceptpms"] == "yes" ? " checked" : "") . " value=yes>All (except blocks)
<input type=radio name=acceptpms" .  ($CURUSER["acceptpms"] == "friends" ? " checked" : "") . " value=friends>Friends only
<input type=radio name=acceptpms" .  ($CURUSER["acceptpms"] == "no" ? " checked" : "") . " value=no>Staff only"
,1);

tr("Delete PMs", "<input type=checkbox name=deletepms" . ($CURUSER["deletepms"] == "yes" ? " checked" : "") . "> Default value for \"Delete PM on reply\"",1);
tr("Save PMs", "<input type=checkbox name=savepms" . ($CURUSER["savepms"] == "yes" ? " checked" : "") . "> Default value for \"Save PM to Sentbox\"",1);
tr("Hide Porn", "<input type=checkbox name=hide_xxx" . ($CURUSER["hide_xxx"] == "yes" ? " checked" : "") . "> Hide porn torrents.",1);
tr("Show Weather", "<input type=checkbox name=view_weather" . ($CURUSER["view_weather"] == "yes" ? " checked" : "") . "> Display the current Vancouver weather on each page.",1);
tr("Hide Friend Feed", "<input type=checkbox name=hide_feed" . ($CURUSER["hide_feed"] == "yes" ? " checked" : "") . "> Hide the friend feed on the front page.",1);

$r = query("SELECT id,name FROM categories ORDER BY name") or sqlerr();
//$categories = "Default browsing categories:<br>\n";
if (mysql_num_rows($r) > 0)
{
	$categories .= "<table><tr>\n";
	$i = 0;
	while ($a = mysql_fetch_assoc($r))
	{
	  $categories .=  ($i && $i % 2 == 0) ? "</tr><tr>" : "";
	  $categories .= "<td class=bottom style='padding-right: 5px'><input name=cat$a[id] type=\"checkbox\" " . (strpos($CURUSER['notifs'], "[cat$a[id]]") !== false ? " checked" : "") . " value='yes'>&nbsp;" . htmlspecialchars($a["name"]) . "</td>\n";
	  ++$i;
	}
	$categories .= "</tr></table>\n";
}

tr("Email notification", "<input type=checkbox name=pmnotif" . (strpos($CURUSER['notifs'], "[pm]") !== false ? " checked" : "") . " value=yes> Notify me when I have received a PM.<br>\n" .
	 "<input type=checkbox name=emailnotif" . (strpos($CURUSER['notifs'], "[email]") !== false ? " checked" : "") . " value=yes> Notify me when a torrent is uploaded in one of <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my default browsing categories.\n"
   , 1);
tr("Browse default<br>categories",$categories,1);
tr("Stylesheet", "<select name=stylesheet>\n$stylesheets</select>",1);
tr("Logo", "<select name=logo>\n$logos</select>",1);
tr("Residence", "<select name=residence>\n$residences</select>",1);
tr("Faculty", "<select name=faculty>\n$faculties</select>",1);
tr("Gender",
"<input type=radio name=gender" . ($CURUSER["gender"] == "N/A" ? " checked" : "") . " value=N/A>N/A
<input type=radio name=gender" . ($CURUSER["gender"] == "Male" ? " checked" : "") . " value=Male>Male
<input type=radio name=gender" .  ($CURUSER["gender"] == "Female" ? " checked" : "") . " value=Female>Female"
,1);
tr("Avatar URL", "<input name=avatar size=50 value=\"" . htmlspecialchars($CURUSER["avatar"]) .
  "\"><br>\nWidth should be 150 pixels (will be resized if necessary)\n<br>If you need a host for the picture, try <a href=\"http://hiderefer.com/?http://www.imageshack.us\" target=_blank>ImageShack</a>.",1);
tr("Torrents per page", "<input type=text size=10 name=torrentsperpage value=$CURUSER[torrentsperpage]> (0=use default setting)",1);
tr("Topics per page", "<input type=text size=10 name=topicsperpage value=$CURUSER[topicsperpage]> (0=use default setting)",1);
tr("Posts per page", "<input type=text size=10 name=postsperpage value=$CURUSER[postsperpage]> (0=use default setting)",1);
tr("View avatars", "<input type=checkbox name=avatars" . ($CURUSER["avatars"] == "yes" ? " checked" : "") . "> (Low bandwidth users might want to turn this off)",1);
tr("Info", "<textarea name=info cols=60 rows=8>" . $CURUSER["info"] . "</textarea><br>Displayed on your public page. May contain <a href=tags.php target=_blank>BB codes</a> and <a href=smilies.php target=_blank>smilies</a>.", 1);
tr("Reset passkey","<input type=checkbox name=resetpasskey value=1 /> (Active torrents must be redownloaded)", 1);
print("<tr><td colspan=\"2\" align=left><b>Note:</b> You must enter your current password to change your password or email address.</td></tr>\n");
tr("Email address", "<input type=\"text\" name=\"email\" size=50 value=\"" . htmlspecialchars($CURUSER["email"]) . "\" />", 1);
tr("Change password", "<input type=\"password\" name=\"chpassword\" size=\"50\" />", 1);
tr("Type password again", "<input type=\"password\" name=\"passagain\" size=\"50\" />", 1);
tr("Current password", "<input type=\"password\" name=\"oldpassword\" size=\"50\" />", 1);

function priv($name, $descr) {
	global $CURUSER;
	if ($CURUSER["privacy"] == $name)
		return "<input type=\"radio\" name=\"privacy\" value=\"$name\" checked=\"checked\" /> $descr";
	return "<input type=\"radio\" name=\"privacy\" value=\"$name\" /> $descr";
}

/* tr("Privacy level",  priv("normal", "Normal") . " " . priv("low", "Low (email address will be shown)") . " " . priv("strong", "Strong (no info will be made available)"), 1); */

?>
<tr><td colspan="2" align="center"><input type="submit" value="Submit changes!" style='height: 25px'> <input type="reset" value="Revert changes!" style='height: 25px'></td></tr>
</table>
</form>
</td>
</tr>
</table>
<?
if ($messages){
  print("<p>You have $messages message" . ($messages != 1 ? "s" : "") . " ($unread new) in your <a href=inbox.php><b>inbox</b></a>,<br>\n");
	if ($outmessages)
		print("and $outmessages message" . ($outmessages != 1 ? "s" : "") . " in your <a href=inbox.php?out=1><b>sentbox</b></a>.\n</p>");
	else
		print("and your <a href=inbox.php?out=1>sentbox</a> is empty.</p>");
}
else
{
  print("<p>Your <a href=inbox.php>inbox</a> is empty, <br>\n");
	if ($outmessages)
		print("and you have $outmessages message" . ($outmessages != 1 ? "s" : "") . " in your <a href=inbox.php?out=1><b>sentbox</b></a>.\n</p>");
	else
		print("and so is your <a href=inbox.php?out=1>sentbox</a>.</p>");
}

print("<p><a href=users.php><b>Find User/Browse User List</b></a></p>");
stdfoot();

?>